package com.module.antiFake.manager;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.antiFake.dao.AntiFakeComplainDao;
import com.module.antiFake.util.AntiFakeBeanUtil;
import com.module.antiFake.util.AntiFakeConstant;
import com.module.antiFake.vo.AntiFakeComplain;
import com.module.antiFake.vo.AntiFakeFile;



public class AntiFakeComplainManager extends GenericHibernateManager<AntiFakeComplain, Integer>{
	private static AntiFakeComplainManager service;
	private AntiFakeComplainDao dao  = AntiFakeComplainDao.getInstance();
	private AntiFakeComplainManager(){
		
	}
	public synchronized static AntiFakeComplainManager getInstance(){
		if(service == null){
			service = new AntiFakeComplainManager();
		}
		return service;
	}
	
	
	/**
	 * 投诉列表
	 * @param bean
	 * @return
	 */
	public Page listAntiFakeComplain(AntiFakeComplain bean,Page page) throws Exception{
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		try {
			page= dao.getList(session,bean, page.getStart(), page.getLimit());
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		}finally{
			this.closeSession();
		}
		return page;
	}
	
	/**
	 * 添加投诉信息
	 * @param antiFakeComplain
	 * @return
	 */
	public boolean addAntiFakeComplain(AntiFakeComplain antiFakeComplain,List<AntiFakeFile> listfile) throws Exception{
		boolean flag=false;
		antiFakeComplain.setState(AntiFakeConstant.AUDIT_STATE);
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		try {			
			dao.saveComplain(this.getSession(),antiFakeComplain,listfile);
			tx.commit();
			flag = true;
		}catch (Exception e) {
			tx.rollback();
			flag=false;
			throw e;
		}finally{
			this.closeSession();
		}	
		return flag;
	}
	
	/**
	 * 根据手机串号获得投诉记录
	 * @param bean
	 */
	@SuppressWarnings("rawtypes")
	public List listAntiFakeComplainByImei(AntiFakeBeanUtil bean) throws Exception{
		try{
			return dao.listAntiFakeComplainByImei(this.getSession(),bean);
		}catch (Exception e) {
			throw e;
		}finally{
			this.closeSession();
		}
	}
	/**
	 * 修改投诉记录的状态
	 * @param id
	 * @return
	 */
	public boolean disposeof(int id,String state) throws Exception{
		AntiFakeComplain antiFakeComplain;
		boolean flag=false;
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		try {
			antiFakeComplain = this.get(id);
			antiFakeComplain.setState(state);
			flag = dao.update(session,antiFakeComplain);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		}finally{
			this.closeSession();
		}
		return flag;
	}
	@Override
	public GenericHibernateDao<AntiFakeComplain, Integer> getDao() {
		return dao;
	}
}
